﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Reviewer.Entities;
using System.Drawing.Imaging;
using System.IO;

public partial class CMSV2_Controls_GameCover : System.Web.UI.UserControl
{
    public int CurrentImageFormatId
    {
        get
        {
            if (ViewState["CurrentImageFormatId"] != null)
            {
                return (int)ViewState["CurrentImageFormatId"];
            }

            return 0;
        }
        set
        {
            ViewState["CurrentImageFormatId"] = value;
        }
    }

    public Guid GameId
    {
        get { return (Guid)ViewState["GameId"]; }
        set { ViewState["GameId"] = value; }
    }

    private Format[] formats;
    public Format[] Formats
    {
        get
        {
            return this.formats;
        }
        set
        {
            this.formats = value;
            this.uiRepCovers.DataSource = formats;
            this.uiRepCovers.DataBind();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void uiRepCovers_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Image img = e.Item.FindControl("uiImgCover") as Image;
            string filename = ResolveUrl("~/Covers/" + this.GameId.ToString() + "_" + (e.Item.DataItem as Format).Id.ToString());
            if (File.Exists(MapPath(filename + ".png")))
            {
                img.ImageUrl = filename + ".png?uid=" + Guid.NewGuid().ToString();
            }
            else
            {
                img.ImageUrl = filename + ".jpg?uid=" + Guid.NewGuid().ToString();
            }
        }
    }

    protected void uiImageUploader_OnImageTransformationComplte(object sender, System.Drawing.Image transformedImage)
    {
        int val = CurrentImageFormatId;
        if (val > 0)
        {
            string filename = ResolveUrl("~/Covers/" + this.GameId.ToString() + "_" + val.ToString() + ".png");
            transformedImage.Save(MapPath(filename), ImageFormat.Png);

            foreach (RepeaterItem item in this.uiRepCovers.Items)
            {
                HiddenField field = item.FindControl("uiHfFormatId") as HiddenField;
                if (field.Value.Equals(val.ToString(), StringComparison.OrdinalIgnoreCase))
                {
                    Image img = item.FindControl("uiImgCover") as Image;
                    img.ImageUrl = filename + "?uid=" + Guid.NewGuid().ToString();
                    break;
                }
            }
        }
    }

    protected void uiBtnChangeCover_Click(object sender, EventArgs e)
    {
        int imageFormatId = Convert.ToInt32(((sender as Button).Parent.FindControl("uiHfFormatId") as HiddenField).Value);
        this.CurrentImageFormatId = imageFormatId;
        this.uiMpePopup.Show();
    }
}
